//
//  ORPageViewController.m
//  News
//
//  Created by rimi on 2016/12/5.
//  Copyright © 2016年 dongfumei. All rights reserved.
//

#import "ORPageViewController.h"

#import "CollectionViewCell.h"
#import "ORModel.h"
#import "ORATableViewCell.h"
#import "DataSourceModel.h"
#import "ORPEViewController.h"
#import "ORsearchViewController.h"

@interface ORPageViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate>


@property (nonatomic, strong) UICollectionView   *dfCollectionView;
@property (nonatomic, strong) UIView             *backView;
@property (nonatomic, strong) NSArray            *dataSource;
@property (nonatomic, strong) NSArray            *dataButton;
@property (nonatomic, strong) UIButton           *backButton;
@property (nonatomic, strong) UITableView        *tableView;
@property (nonatomic, strong) NSArray            *datail;
@property (nonatomic, strong) NSString           *strId;
@property (nonatomic, strong) UILabel            *errorLabel;

@end

@implementation ORPageViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if ( [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
        self.tableView.tableHeaderView = self.errorLabel;
    } else {
        
        [self confinViewS];
        self.tableView.tableHeaderView = self.dfCollectionView;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"分类";
    self.view.backgroundColor = [UIColor whiteColor];
    [self confinViewS];
}

- (void)confinViewS{
    
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationItem.title=@"分类";
    [self.view addSubview:self.backView];
    [self.view addSubview:self.backButton];
    [self initaliApprecanPage];
    [self.view addSubview:self.tableView];
    
    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"搜索.jpg"] style:UIBarButtonItemStyleDone target:self action:@selector(searView:)];
    
}

- (void)searView:(UIBarButtonItem *)sender{
    
    ORsearchViewController  *vc=[[ORsearchViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - *** 分类网络请求数据 ***
- (void)initaliApprecanPage{
    
    NSString *uRl = @"http://api.jisuapi.com/news/channel";
    
    NSDictionary  *dic = @{@"appkey":@"8624d50a80511fb6"};
    
    [ORNetWorkRequestManager getMethod:uRl withParames:dic Success:^(id respondObject) {
        self.dataSource = respondObject[@"result"];
        self.strId = self.dataButton[0][@"result"];
        
        __weak typeof(self) weakSelf = self;
        dispatch_async(dispatch_get_main_queue(), ^{
            
        [weakSelf initaliApprecanDatasource:respondObject[@"result"]];
        [weakSelf.dfCollectionView reloadData];
            [weakSelf.tableView reloadData];
            
        NSInteger selectedIndex = 0;
        NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
            
        [self.dfCollectionView selectItemAtIndexPath:selectedIndexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
            
        });
        
        NSLog(@"%@",respondObject);
        
    } Failure:^(NSError *error) {
        
    }];
}

#pragma mark -  ** tableView 数据 ***
- (void)initaliApprecanDatasource:(NSString *)channel{
    
    NSString *uRl=@"http://api.jisuapi.com/news/get";
    NSDictionary *dic=@{@"channel":channel,@"num":@"20",@"appkey":@"8624d50a80511fb6"};
    [ORNetWorkRequestManager getMethod:uRl withParames:dic Success:^(id respondObject) {
        NSArray *array=respondObject[@"result"][@"list"];
//        NSLog(@"%@",respondObject);
        self.datail = [DataSourceModel modelArrayFromJsonArray:array];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } Failure:^(NSError *error) {
        
    }];
    
    
}

#pragma mark -  集合视图
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [self initaliApprecanDatasource:self.dataSource[indexPath.item]];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataSource.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CollectionViewCell  *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    cell.nameLable.text = self.dataSource[indexPath.item];
    
    return cell;
}

#pragma mark - *** TableView***
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datail.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *string=@"UITableView";
    ORATableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell=[[ORATableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:string];
    }
    cell.selectionStyle=UITextAutocapitalizationTypeNone;
    
    DataSourceModel *model=self.datail[indexPath.row];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:@"4"] options:SDWebImageProgressiveDownload];
    cell.titleLabel.text =[NSString stringWithFormat:@"  %@", model.title];
    cell.contentLabel.text =model.src;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ORPEViewController  *vc = [[ORPEViewController alloc]init];
    DataSourceModel   *model = self.datail[indexPath.row];
    
    [[Clickbrowse sharedClickbrowseDataModel].clickbrowseS addObject:model];
    [[Clickbrowse sharedClickbrowseDataModel] save];
    
    NSString *url = model.url;
    vc.iDSurl = url;
    
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}



#pragma mark -  getter 与 setter
- (UICollectionView *)dfCollectionView{
    
    if (!_dfCollectionView) {
        
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = AAdaptationSize(60, 50);
        
        layout.minimumInteritemSpacing = 2;
        layout.minimumLineSpacing = -10;
        [layout setSectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
        _dfCollectionView = [[UICollectionView alloc]initWithFrame:AAdaptionRect(14, 50, 386, 150)collectionViewLayout:layout];
        _dfCollectionView.backgroundColor=[UIColor whiteColor];
        _dfCollectionView.delegate = self;
        _dfCollectionView.dataSource = self;
        _dfCollectionView.scrollEnabled = NO;
        
        [_dfCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell"bundle:nil]forCellWithReuseIdentifier:@"CollectionViewCell"];
    }
    return _dfCollectionView;
}

- (UIView *)backView{
    
    if (!_backView) {
        _backView=[[UIView alloc]initWithFrame:AAdaptionRect(0, 100, 386, 120)];
        _backView.backgroundColor=[UIColor yellowColor];
        
    }
    
    return _backView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:AAdaptionRect(0, 0, kBaseWidth, kBaseHeight - 128) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = AAdaptation(250);
        _tableView.tableFooterView=[[UIView alloc]init];
        _tableView.tableHeaderView=self.dfCollectionView;
    }
    return _tableView;
}

-(UILabel *)errorLabel{
    if (!_errorLabel) {
        _errorLabel = [[UILabel alloc]initWithFrame:AAdaptionRect(0, 0, kBaseWidth, 40)];
        _errorLabel.backgroundColor = [UIColor colorWithRed:248/255.f green:200/255.f blue:52/255.f alpha:0.6];
        _errorLabel.textAlignment = NSTextAlignmentCenter;
        _errorLabel.text = @"你已进入二次元!";
    }
    return _errorLabel;
}

@end
